package ATMproject;

import javax.swing.*;
import java.awt.*;
import java.io.*;

public class Util {
    public static Image getImage(String path) {
        return new ImageIcon(jframe.Util.class.getResource(path)).getImage();
    }

    public static ImageIcon getIcon(String path){
        return new ImageIcon(jframe.Util.class.getResource(path));
    }


    /**
     * 序列化对象
     *
     * @param path 保存序列化对象的路径
     * @param obj  序列化对象
     */
    public static void writeObject(String path, Object obj) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;

        try {
            fos = new FileOutputStream(path);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 反序列化对象
     *
     * @param path 反序列化对象的文件路径
     * @return
     */
    public static Object readObject(String path) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;

        try {
            fis = new FileInputStream(path);
            ois = new ObjectInputStream(fis);
            return ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}